<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('pos.swiftpay.SOId')" type="remoteSelect" v-model="baTable.form.items!.SOId" prop="SOId" :input-attr="{ pk: 'cncerp_stock_locations.loccode', field: 'locationname', 'remote-url': '/admin/stock.Locations/index' }" :placeholder="t('Please select field', { field: t('pos.swiftpay.SOId') })" />
                    <FormItem :label="t('pos.swiftpay.PayType')" type="remoteSelect" v-model="baTable.form.items!.PayType" prop="PayType" :input-attr="{ pk: 'cncerp_pay_trade_type.pay_type', field: 'pay_name', 'remote-url': '/admin/pay.trade.Type/index' }" :placeholder="t('Please select field', { field: t('pos.swiftpay.PayType') })" />
                    <FormItem :label="t('pos.swiftpay.IsSubscribe')" type="string" v-model="baTable.form.items!.IsSubscribe" prop="IsSubscribe" :placeholder="t('Please input field', { field: t('pos.swiftpay.IsSubscribe') })" />
                    <FormItem :label="t('pos.swiftpay.BankType')" type="remoteSelects" v-model="baTable.form.items!.BankType" prop="BankType" :input-attr="{ pk: 'cncerp_pay_bank_type.bank_type', field: 'bank_name', 'remote-url': '/admin/pay.bank.Type/index' }" :placeholder="t('Please select field', { field: t('pos.swiftpay.BankType') })" />
                    <FormItem :label="t('pos.swiftpay.TransAmount')" type="number" prop="TransAmount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.TransAmount" :placeholder="t('Please input field', { field: t('pos.swiftpay.TransAmount') })" />
                    <FormItem :label="t('pos.swiftpay.TransCode')" type="string" v-model="baTable.form.items!.TransCode" prop="TransCode" :placeholder="t('Please input field', { field: t('pos.swiftpay.TransCode') })" />
                    <FormItem :label="t('pos.swiftpay.TransTime')" type="datetime" v-model="baTable.form.items!.TransTime" prop="TransTime" :placeholder="t('Please select field', { field: t('pos.swiftpay.TransTime') })" />
                    <FormItem :label="t('pos.swiftpay.PosOrderId')" type="number" prop="PosOrderId" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.PosOrderId" :placeholder="t('Please input field', { field: t('pos.swiftpay.PosOrderId') })" />
                    <FormItem :label="t('pos.swiftpay.PosOrderCode')" type="string" v-model="baTable.form.items!.PosOrderCode" prop="PosOrderCode" :placeholder="t('Please input field', { field: t('pos.swiftpay.PosOrderCode') })" />
                    <FormItem :label="t('pos.swiftpay.PosThirdOrderCode')" type="string" v-model="baTable.form.items!.PosThirdOrderCode" prop="PosThirdOrderCode" :placeholder="t('Please input field', { field: t('pos.swiftpay.PosThirdOrderCode') })" />
                    <FormItem :label="t('pos.swiftpay.CreateTime')" type="datetime" v-model="baTable.form.items!.CreateTime" prop="CreateTime" :placeholder="t('Please select field', { field: t('pos.swiftpay.CreateTime') })" />
                    <FormItem :label="t('pos.swiftpay.TransType')" type="number" prop="TransType" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.TransType" :placeholder="t('Please input field', { field: t('pos.swiftpay.TransType') })" />
                    <FormItem :label="t('pos.swiftpay.TransNo')" type="number" prop="TransNo" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.TransNo" :placeholder="t('Please input field', { field: t('pos.swiftpay.TransNo') })" />
                    <FormItem :label="t('pos.swiftpay.OpenId')" type="string" v-model="baTable.form.items!.OpenId" prop="OpenId" :placeholder="t('Please input field', { field: t('pos.swiftpay.OpenId') })" />
                    <FormItem :label="t('pos.swiftpay.TradeState')" type="string" v-model="baTable.form.items!.TradeState" prop="TradeState" :placeholder="t('Please input field', { field: t('pos.swiftpay.TradeState') })" />
                    <FormItem :label="t('pos.swiftpay.Attach')" type="string" v-model="baTable.form.items!.Attach" prop="Attach" :placeholder="t('Please input field', { field: t('pos.swiftpay.Attach') })" />
                    <FormItem :label="t('pos.swiftpay.SettleTime')" type="datetime" v-model="baTable.form.items!.SettleTime" prop="SettleTime" :placeholder="t('Please select field', { field: t('pos.swiftpay.SettleTime') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { FormInstance, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'

const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    TransAmount: [buildValidatorData({ name: 'number', title: t('pos.swiftpay.TransAmount') })],
    TransTime: [buildValidatorData({ name: 'date', title: t('pos.swiftpay.TransTime') })],
    PosOrderId: [buildValidatorData({ name: 'number', title: t('pos.swiftpay.PosOrderId') })],
    CreateTime: [buildValidatorData({ name: 'date', title: t('pos.swiftpay.CreateTime') })],
    TransType: [buildValidatorData({ name: 'number', title: t('pos.swiftpay.TransType') })],
    TransNo: [buildValidatorData({ name: 'number', title: t('pos.swiftpay.TransNo') })],
    SettleTime: [buildValidatorData({ name: 'date', title: t('pos.swiftpay.SettleTime') })],
})
</script>

<style scoped lang="scss"></style>
